package com.ikas.ai.server.module.alarm.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikas.ai.server.error.BusinessException;
import com.ikas.ai.server.module.alarm.dao.TripAlarmDAO;
import com.ikas.ai.server.module.alarm.model.TripAlarm;
import com.ikas.ai.server.module.alarm.model.enums.AlarmType;
import com.ikas.ai.server.module.data.service.TripMeteService;
import com.ikas.ai.server.module.instance.service.DorisWorkFlowInstanceService;
import com.ikas.ai.server.module.instance.service.DorisWorkInstanceStepDetailService;
import com.ikas.ai.server.module.instance.service.DorisWorkInstanceStepService;
import com.ikas.ai.utils.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class TripAlarmService extends ServiceImpl<TripAlarmDAO, TripAlarm> {
    @Autowired
    AlarmCacheService alarmCacheService;
    @Autowired
    TripMeteService tripMeteService;

    @Transactional(rollbackFor = Exception.class)
    public void solved(Long id, String userName) {
        TripAlarm tripAlarm = getById(id);
        if (ObjectUtil.isNull(tripAlarm)) {
            throw new BusinessException("该ID找不到对应条");
        }
        if (tripAlarm.getSolved().equals(TripAlarm.ALARM_SOLVED)) {
            throw new BusinessException("该告警已经消缺");
        }
        update(
                new UpdateWrapper<TripAlarm>().eq("id", id)
                        .set("solved", TripAlarm.ALARM_SOLVED)
                        .set("solve_user", userName)
                        .set("solve_time", new Date())
        );
        int count = count(
                new LambdaQueryWrapper<TripAlarm>()
                        .eq(TripAlarm::getSolved, TripAlarm.ALARM_NOT_SOLVED)
                        .eq(TripAlarm::getMachineNo, tripAlarm.getMachineNo())
        );
        alarmCacheService.updateAlarm(AlarmType.TRIP, tripAlarm.getMachineNo(), count);
    }

    public List<String> getAlarmDescriptions() {
        return list().stream().map(TripAlarm::getMeteName).distinct().collect(Collectors.toList());
    }

    public List<TripAlarm> page(String machineNo, String description, Integer solved, Date startTime, Date endTime) {
        Long startTimestamp = null;
        Long endTimestamp = null;
        if (startTime != null) {
            startTimestamp = startTime.getTime();
        }
        if (endTime != null) {
            endTimestamp = endTime.getTime();
        }
        return list(
                new LambdaQueryWrapper<TripAlarm>()
                        .eq(StringUtils.isNotBlank(machineNo), TripAlarm::getMachineNo, machineNo)
                        .like(StringUtils.isNotBlank(description), TripAlarm::getMeteName, description)
                        .eq(!ObjectUtil.isNull(solved), TripAlarm::getSolved, solved)
                        .gt(!ObjectUtil.isNull(startTime), TripAlarm::getTripTime, startTimestamp)
                        .lt(!ObjectUtil.isNull(endTime), TripAlarm::getTripTime, endTimestamp)
        );
    }

    @Transactional
    public void reOpenForTest(Long id) {
        TripAlarm tripAlarm = getById(id);
        tripAlarm.setSolved(TripAlarm.ALARM_NOT_SOLVED);
        tripAlarm.setSolveUser("");
        updateById(tripAlarm);
        int notSolved = count(
                new LambdaQueryWrapper<TripAlarm>()
                        .eq(TripAlarm::getSolved, TripAlarm.ALARM_NOT_SOLVED)
                        .eq(TripAlarm::getMachineNo, tripAlarm.getMachineNo())
        );
        alarmCacheService.updateAlarm(AlarmType.TRIP, tripAlarm.getMachineNo(), notSolved);
    }
}
